import wx
import os
class MainWindow(wx.Frame):
    """We simply derive a new class of Frame. """
    def __init__(self,parent,title):
        # A "-1" in the size parameter instructs wxWidgets to use the default size.
        wx.Frame.__init__(self, parent, title=title, size=(-1,-1))
        self.control = wx.TextCtrl(self,style=wx.TE_MULTILINE)
     #   self.Show(True)


        #setting up the menu
        filemenu = wx.Menu()
        #wx.ID_ABOUT and wx.ID_EXIT is satand IDs peovided by wxWidgets.
        menuAbout = filemenu.Append(wx.ID_ABOUT, "&About", " Infomation about this program")
        filemenu.AppendSeparator()
        menuExit = filemenu.Append(wx.ID_EXIT, "E&xit", "Terminate the program")
        menuOpen = filemenu.Append(wx.ID_OPEN, "&Open", "Open a file")
        
        #creating the menuBar.
        menuBar = wx.MenuBar()
        menuBar.Append(filemenu,"&file")#Adding the fulemenu to the MenuBar
        self.SetMenuBar(menuBar)#Adding the MenuBar to the Frame content.
        #self.Show(True)
        
        #Set Event
        self.Bind(wx.EVT_MENU, self.OnAbout, menuAbout)
        self.Bind(wx.EVT_MENU, self.OnExit, menuExit)
        self.Bind(wx.EVT_MENU, self.OnOpen, menuOpen)

        self.sizer2 = wx.BoxSizer(wx.VERTICAL)
        self.buttons = []
        for i in range(0,6):
            self.buttons.append(wx.Button(self, -1, "Button &" + str(i)))
            self.sizer2.Add(self.buttons[i], 1,wx.EXPAND )
        #use some sizers to see layout options
        self.sizer = wx.BoxSizer(wx.HORIZONTAL )
        self.sizer.Add(self.control, 1,wx.EXPAND)
        self.sizer.Add(self.sizer2,0,wx.EXPAND)

        #Layout sizers
        self.SetSizer(self.sizer)#tells your window (or frame) which sizer to use
        self.SetAutoLayout(True)#tells your window to use the sizer to position and size your components
        self.sizer.Fit(self)#tells the sizer to calculate the initial size and position for all its elements
        self.Show()
    def OnAbout(self,e):
        #A messag dialog box with an OK button, wx.OK is a standart ID in wxWidgets.
        dlg = wx.MessageDialog(self, "A small text editor", "About Sample Editor")
        dlg.ShowModal() #Show it
        dlg.Destroy() #finally destroy it when finished

    def OnExit(self,e):
        self.Close(True)# close the Frame
    def OnOpen(self,e):
        """Open a file"""
        self.dirname = ''
        dlg = wx.FileDialog(self,"Choose a file", self.dirname, "", "*.*",wx.OPEN)
        if dlg.ShowModal() == wx.ID_OK:
            self.filename = dlg.GetFilename()
            self.dirname = dlg.GetDirectory()
            f = open(os.path.join(self.dirname, self.filename), 'r')
            self.control.SetValue(f.read())
            f.close
        dlg.Destroy()
app = wx.App(False)
frame = MainWindow(None, 'Sample Editor')
app.MainLoop()
